﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 21/12/2008
// Time: 12:24
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: Country.cs
#endregion

using System;
using System.Data;
using csharp.common;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Country.
	/// </summary>
	public class Country : DefaultClass, IClass
	{
		#region private declaration
			private String _name;
			private String _create_date;
			private String _enable;
		#endregion
		
		public Country()
		{
			clear();
			gsql.table = "common.country";
			_sequence  = "common.country_country_id_seq";
		}
		
		#region properties
			public string name 
			{
				get { return _name; }
				set { _name = value; }
			}
			
			public string createDate 
			{
				get { return _create_date; }
				set { _create_date = value; }
			}
			
			public Boolean enable 
			{
				get { return Convert.ToBoolean(_enable); }
				set { _enable = Convert.ToString(value); }
			}	
		#endregion
	
		public void clear()
		{
			_id          = String.Empty;
			_name        = String.Empty;
			_create_date = String.Empty;
			_enable		 = Boolean.TrueString;			
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id,    	"country_id");
			gsql.setValue(_name,  	"country_name");
			//gsql.setValue(_create_date, "country_create_date");
			gsql.setValue(_enable,      "country_enable");
		}
		
		public override void get(DataRow dr)
		{
			_id      	= dr["country_id"].ToString();
			_name       = dr["country_name"].ToString();
			_create_date= Convert.ToDateTime(dr["country_create_date"]).ToString(common.CONSTANTS.dateFormatBrazilian);
			_enable     = dr["country_enable"].ToString();
		}
	}

	public class Countries : DefaultList, IList
	{
		public Countries()
		{
			gsql.table = "common.country";
		}
		
		/*public List<Country> items
		{
			get { return items; }
		}*/		
				
		public Country get(Int32 index)
		{
			return (Country)items[index];
		}
		
		public Country get()
		{
			return (Country)items[index];
		}		
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY country_name";
			return mountList();
		}
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam = gsql.getRowCount();
			items.Clear();
			for (int i=0; i < tam; i++)
			{
				Country c = new Country();
				c.get(gsql.getRow(i));
				items.Add(c);
			}
			return true;
		}
	}
}
